/**
 * 
 */
package com.leyu.search.service;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * @author change
 * 
 */
public class SearchQueryParam implements Serializable {
	public static final int ARRAY_SIZE = 13;
	public boolean facetQuery;
	public Long cateId1=0L;
	public Long cateId2=0L;
	public Long cateId3=0L;
	public Long areaId=0L;
	public Long brandId=0L;
	public Long priceRangeId=0L;
	public Long systemId=0L;
	public Long screenSizeId=0L;
	public Long specialId=0L;
	public Long sortId=0L;
	public Long tempId1=0L;
	public Long tempId2=0L;
	public String keywords;
	public Integer pageNum;
	public Integer pageSize = 40;

	public boolean onlyKeywords;
	private String paramsArray[] = new String[ARRAY_SIZE];
	private static final String []keywordFields={"area","brand","system","name", "specials","keywords"};

	public SearchQueryParam() {
		for(int i=0;i<paramsArray.length;i++){
			paramsArray[i]="0";
		}
	}

	public SearchQueryParam(String[] arrays) {
		for(int i=0;i<paramsArray.length;i++){
			paramsArray[i]="0";
		}
		if (arrays == null || arrays.length <= 0)
			return;
		System.arraycopy(arrays, 0, paramsArray, 0, arrays.length);
		this.cateId1 = Long.valueOf(arrays[0]);
		if (arrays.length == 1)
			return;
		this.cateId2 = Long.valueOf(arrays[1]);
		if (arrays.length == 2)
			return;
		this.cateId3 = Long.valueOf(arrays[2]);
		if (arrays.length == 3)
			return;
		this.areaId = Long.valueOf(arrays[3]);
		if (arrays.length == 4)
			return;
		this.brandId = Long.valueOf(arrays[4]);
		if (arrays.length == 5)
			return;
		this.priceRangeId = Long.valueOf(arrays[5]);
		if (arrays.length == 6)
			return;
		this.systemId = Long.valueOf(arrays[6]);
		if (arrays.length == 7)
			return;
		this.screenSizeId = Long.valueOf(arrays[7]);
		if (arrays.length == 8)
			return;
		this.specialId = Long.valueOf(arrays[8]);
		if (arrays.length == 9)
			return;
		this.sortId = Long.valueOf(arrays[9]);
		if (arrays.length == 10)
			return;
		this.tempId1 = Long.valueOf(arrays[10]);
		if (arrays.length == 11)
			return;
		this.tempId2 = Long.valueOf(arrays[11]);
		if (arrays.length == 12)
			return;
		this.pageNum = Integer.valueOf(arrays[12]);

	}

	public SearchQueryParam(String res) {
		this(res.split("_"));
	}

	public void setParamsArray(String[] paramsArray) {
		this.paramsArray = paramsArray;
		int i=0;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.cateId1=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.cateId2=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.cateId3=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.areaId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.brandId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.priceRangeId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.systemId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.screenSizeId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.specialId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.sortId=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.tempId1=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.tempId2=Long.valueOf(paramsArray[i]);
		}
		i++;
		if(StringUtils.isNotBlank(paramsArray[i])&&!"null".equalsIgnoreCase(paramsArray[i])){
			this.pageNum= Integer.valueOf(paramsArray[i]);
		}
	}

	public String[] getParamsArray() {
		if (paramsArray == null) {
			paramsArray = new String[ARRAY_SIZE];
		}
		paramsArray[0] = this.cateId1 + "";
		paramsArray[1] = this.cateId2 + "";
		paramsArray[2] = this.cateId3 + "";
		paramsArray[3] = this.areaId + "";
		paramsArray[4] = this.brandId + "";
		paramsArray[5] = this.priceRangeId + "";
		paramsArray[6] = this.systemId + "";
		paramsArray[7] = this.screenSizeId + "";
		paramsArray[8] = this.specialId + "";
		paramsArray[9] = this.sortId + "";
		paramsArray[10] = this.tempId1 + "";
		paramsArray[11] = this.tempId2 + "";
		paramsArray[12] = this.pageNum + "";
		return paramsArray;
	}

	public String toHref(String arrays[]) {
		StringBuilder sb = new StringBuilder();
		for (String item : arrays) {
			if (StringUtils.isBlank(item)) {
				sb.append("0").append("_");
			} else {
				sb.append(item).append("_");
			}

		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	public String toAndQueryString() {
		StringBuilder sb = new StringBuilder();
		Map<String, Object> contents = new LinkedHashMap<String, Object>();
		contents.put("cateId1", cateId1);
		contents.put("cateId2", cateId2);
		contents.put("cateId3", cateId3);
		contents.put("areaId", areaId);
		contents.put("brandId", brandId);
		contents.put("priceRangeId", priceRangeId);
		contents.put("systemId", systemId);
		contents.put("screenSizeId", screenSizeId);
		contents.put("specialId", specialId);
		
		boolean flag = false;
		for (Map.Entry<String, Object> entry : contents.entrySet()) {
			if (entry.getValue() == null
					|| Long.valueOf(entry.getValue().toString()) <= 0)
				continue;
			if (!flag) {
				sb.append(entry.getKey()).append(":").append(entry.getValue());
				flag = true;
			} else {

				sb.append(" AND ").append(entry.getKey()).append(":")
						.append(entry.getValue());
			}
		}
		if(StringUtils.isNotBlank(keywords)){
			flag=true;
			if(sb.length()>0){
				sb.append(" AND (");
				boolean first=false;
				for(String field:keywordFields){
					if(!first){
						first=true;
						sb.append(field).append(":").append(keywords);
					}else{
						sb.append(" OR ").append(field).append(":").append(keywords);
					}
				}
				sb.append(")");
			}else{ 
				boolean first=false;
				for(String field:keywordFields){
					if(!first){
						first=true;
						sb.append(field).append(":").append(keywords);
					}else{
						sb.append(" OR ").append(field).append(":").append(keywords);
					}
				} 
			}
		}
		if (!flag) {
			return "*:*";
		}
		return sb.toString();
	}

	public boolean isOnlyKeywords() {
		return onlyKeywords;
	}

	public void setOnlyKeywords(boolean onlyKeywords) {
		this.onlyKeywords = onlyKeywords;
	}

	public static void main(String args[]) {
		System.out.println(Long.valueOf("00000"));
	}
}
